package com.example.automatic.abs_video;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DYAccessibilityService2 extends AccessibilityService {

    private Timer mTimer = null;

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.e("TAG", "onServiceConnected=========================");
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
    @Override
    public void onAccessibilityEvent(final AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();

        final List<AccessibilityNodeInfo> nodeInfoList = nodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme.lite:id/cu_");
        if (nodeInfoList.size() == 0) {
            return;
        }

//        if (nodeInfoList.size() != 0) {
//            AccessibilityNodeInfo nodeInfo1 = nodeInfoList.get(0);
//            Rect rect = new Rect();
//            nodeInfo1.getBoundsInScreen(rect);
//            Log.e("TAG", "onAccessibilityEvent=================" + rect.toString() + ",," + nodeInfo1.getClassName());
//        }
        if (mTimer == null) {
            mTimer = new Timer();
            mTimer.schedule(new TimerTask() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void run() {
                    AccessibilityNodeInfo nodeInfo = nodeInfoList.get(0);
                    RegionSpace regionSpace = getRegionSpace(nodeInfo);
                    Path path = new Path();
                    path.moveTo(regionSpace.getXStart(), regionSpace.getYStart());
                    path.lineTo(regionSpace.getXEnd(), regionSpace.getYEnd());
                    GestureDescription.Builder builder = new GestureDescription.Builder();
                    GestureDescription.StrokeDescription strokeDescription = new GestureDescription.StrokeDescription(path, regionSpace.getTimeStart(), 10L);
                    GestureDescription gestureDescription = builder.addStroke(strokeDescription).build();
                    dispatchGesture(gestureDescription, new GestureResultCallback() {
                        @Override
                        public void onCompleted(GestureDescription gestureDescription) {
                            super.onCompleted(gestureDescription);
                        }

                        @Override
                        public void onCancelled(GestureDescription gestureDescription) {
                            super.onCancelled(gestureDescription);
                        }
                    }, null);
                    Log.e("TAG", "onAccessibilityEvent=========================2");
                }
            }, 3000, getNum(3, 10) * 1000);
        }


    }

    @Override
    public void onInterrupt() {
        Log.e("TAG", "onInterrupt=========================");
    }


    /**
     * 获取屏幕中的节点边界(可滑动平面区域)
     */
    public static RegionSpace getRegionSpace(AccessibilityNodeInfo nodeInfo) {
        Rect rect = new Rect();
        nodeInfo.getBoundsInScreen(rect);

        //随机缩小百分比范围值
        int xPercent = (int) Math.round(rect.right * (getNum(5, 10) * 0.01));
        int yPercent = (int) Math.round(rect.bottom * (getNum(10, 20) * 0.01));

        Log.e("TAG", xPercent + "," + yPercent);

        //缩小默认平面区域范围
        int left = rect.left + xPercent;
        int top = rect.top + yPercent;
        int right = rect.right - xPercent;
        int bottom = rect.bottom - yPercent;

        Log.e("TAG", left + "," + top + "," + right + "," + bottom);

        //在平面区域中随机获取起始坐标
        int xStart = getNum(left, right);
        int yStart = getNum(top, bottom);
        //在平面区域中随机获取结束坐标
        int xEnd = getNum(xStart - 50, xStart + 50);
        int yEnd;
        if (top > yStart - 100) {
            yEnd = yStart - 100;
        } else {
            yEnd = getNum(top, yStart - 100);
        }
        //两点坐标连接的开始时间与持续时间
        long timeStart = Math.round(getNum(50, 100));
        long duration = Math.round((yStart - yEnd) * 0.1);

        RegionSpace space = new RegionSpace();
        space.setXStart(xStart);
        space.setYStart(yStart);
        space.setXEnd(xEnd);
        space.setYEnd(yEnd);
        space.setTimeStart(timeStart);
        space.setDuration(duration);

        return space;
    }

    /**
     * 随机生成两个坐标点(开始，结束)
     */
    private void space() {

    }

    /**
     * 生成一个0 到 count 之间的随机数
     *
     * @param endNum 结束
     * @return 结果
     */
    public static int getNum(int endNum) {
        if (endNum > 0) {
            Random random = new Random();
            return random.nextInt(endNum);
        }
        return 0;
    }

    /**
     * 生成一个startNum 到 endNum之间的随机数(不包含endNum的随机数)
     *
     * @param startNum 开始
     * @param endNum   结束
     * @return 结果
     */
    public static int getNum(int startNum, int endNum) {
        if (endNum > startNum) {
            Random random = new Random();
            return random.nextInt(endNum - startNum) + startNum;
        }
        return 0;
    }

    public static class RegionSpace {
        int xStart = 0;
        int yStart = 0;
        //在平面区域中随机获取结束坐标
        int xEnd = 0;
        int yEnd = 0;
        //两点坐标连接的开始时间与持续时间
        long timeStart = 0L;
        long duration = 0L;

        public int getXStart() {
            return xStart;
        }

        public void setXStart(int xStart) {
            this.xStart = xStart;
        }

        public int getYStart() {
            return yStart;
        }

        public void setYStart(int yStart) {
            this.yStart = yStart;
        }

        public int getXEnd() {
            return xEnd;
        }

        public void setXEnd(int xEnd) {
            this.xEnd = xEnd;
        }

        public int getYEnd() {
            return yEnd;
        }

        public void setYEnd(int yEnd) {
            this.yEnd = yEnd;
        }

        public long getTimeStart() {
            return timeStart;
        }

        public void setTimeStart(long timeStart) {
            this.timeStart = timeStart;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        @Override
        public String toString() {
            return "RegionSpace{" +
                    "xStart=" + xStart +
                    ", yStart=" + yStart +
                    ", xEnd=" + xEnd +
                    ", yEnd=" + yEnd +
                    ", timeStart=" + timeStart +
                    ", duration=" + duration +
                    '}';
        }
    }
}
